#include <stdio.h>

#include "pseudocode.h"
#include "tokens.h"
#include "keywords.h"

char *convertfloat2ieee(char *lexval,char *floatlexval){
  float f = strtof(lexval,NULL);
  
  sprintf(floatlexval,"0x%8.8X",*(int*)&f);
  
  return floatlexval;
}

void funcgen(char* funcName){
  /** */
  fprintf(stdout, "\t.globl %s\n",funcName);
  fprintf(stdout, "\t.type %s,@function\n\n",funcName);
  fprintf(stdout, "%s: \n",funcName);
  fprintf(stdout, "\tpushl %%ebx\n");
  fprintf(stdout, "\tmovl (%%esp),%%ebx\n");
  /** */
}

void endfuncgen(){
  fprintf(stdout, "\tmovl %%ebx,(%%esp)\n");
  fprintf(stdout, "\tpopl %%ebx\n");
}

void exitgen(){
  fprintf(stdout, "\tmovl $1,%%eax\n");
  fprintf(stdout, "\tmovl $0,%%ebx\n");
  fprintf(stdout, "\tint $0x80\n");
}

void retgen(){
  fprintf(stdout, "\tret\n");
}

void lblgen(int lbl){
  /** */fprintf(stdout, ".L%d:\n", lbl);/** */
}

void jzgen(int lbl,int aux){
    switch(aux){
    case '=':
      /** */fprintf(stdout, "\tjz .L%d\n", lbl);/** */
      break;
    case '<':
      /** */fprintf(stdout, "\tjl\t.L%d\n", lbl);/** */
      break;
    case '>':
      /** */fprintf(stdout, "\tjg\t.L%d\n", lbl);/** */
      break;
    case GEQ:
      /** */fprintf(stdout, "\tjge\t.L%d\n", lbl);/** */
      break;
    case LEQ:
      /** */fprintf(stdout, "\tjle\t.L%d\n", lbl);/** */
      break;
    case NEQ:
      /** */fprintf(stdout, "\tjnz .L%d\n", lbl);/** */
      break;
  }
}

void jmpgen(int lbl){
  /** */fprintf(stdout, "\tjmp .L%d\n", lbl);/** */
}

void relopgen()
{
  /** */
  fprintf(stdout, "\tpopl\t%%eax\n");
  fprintf(stdout, "\tcmpl\t%%eax,(%%esp)\n");
  /** */
}

void addopgen(int aux)
{  
  switch(aux){
    case OR:
      /** */fprintf(stdout, "\torl\t(%%esp), %%eax\n");/** */
      break;
    case '+':
      if(acctype == 1)
	/** */fprintf(stdout, "\taddl\t(%%esp), %%eax\n");/** */
      else if(acctype == 2)
	/** */fprintf(stdout, "\tfadds\t(%%esp), %%eax\n");/** */
      break;
    case '-':
      if(acctype == 1)
	/** */fprintf(stdout, "\tsubl\t(%%esp), %%eax\n");/** */
      else if(acctype == 2)
	/** */fprintf(stdout, "\tfsubs\t(%%esp), %%eax\n");/** */
      break;
  }
  
  /** */fprintf(stdout, "\tpopl\t%%eax\n");/** */
}

void mulopgen(int aux)
{  
  switch(aux){
    case AND:
      if(acctype == 1)
	/** */fprintf(stdout, "\tandl\t(%%esp), %%eax\n");/** */
      break;
    case '*':
      if(acctype == 1){
	fprintf(stdout, "\tcltd\n");
	/** */fprintf(stdout, "\timull\t(%%esp)\n");/** */
      }else if(acctype == 2)
	/** */fprintf(stdout, "\tfmuls\t(%%esp)\n");/** */
      break;
    case '/':
      if(acctype == 1){
      /** */
      fprintf(stdout, "\tcltd\n");
      fprintf(stdout, "\tidivl\t(%%esp)\n");
      /** */
      }else if(acctype == 2)
	/** */fprintf(stdout, "\tfdivs\t(%%esp)\n");/** */
      break;
    case DIV:
      /** */fprintf(stdout, "\tidivl\t(%%esp)\n");/** */
      break;
    case MOD:
      /** */
      fprintf(stdout, "\tidivl\t(%%esp)\n");
      fprintf(stdout, "\tmovl\t%%edx, %%eax\n");
      /** */
      break;
  }
  
  /** */fprintf(stdout, "\tpopl\t%%eax\n");/** */
}